package renguanyu.app.auth4fx.controller.role;

import javafx.concurrent.Task;
import renguanyu.app.auth4fx.model.Page;
import renguanyu.app.auth4fx.model.role.RoleDto;
import renguanyu.app.auth4fx.model.role.RoleVo;
import renguanyu.app.auth4fx.service.role.RoleService;
import renguanyu.app.auth4fx.util.PageUtil;
import renguanyu.app.auth4fx.view.role.RoleListView;

import java.util.List;
import java.util.Map;

public class RoleListQueryController extends Task<Void> {

    private RoleListView roleListView;
    public RoleListQueryController(RoleListView roleListView){
        this.roleListView = roleListView;
    }

    public void startAsyncQuery() {
        System.out.println("RoleListQuery.execute");
        Thread thread = new Thread(this);
        thread.setDaemon(true);
        thread.start();
    }

    Page page;
    @Override
    protected Void call() {
        System.out.println("RoleListQuery.call");
        RoleDto roleDto = buildRoleDto();
        page = RoleService.getRolePage(roleDto);
        return null;
    }

    private RoleDto buildRoleDto() {
        // 接收参数
        Integer pageSize = roleListView.pageSizeProperty.get();
        Integer currentPageIndex = roleListView.currentPageIndexProperty.get();
        String searchKey = roleListView.searchFieldKey.getValue();
        String searchValue = roleListView.searchFieldValue.getValue();
        // 格式化参数
        String name = "";
        if ("角色名".equals(searchKey) && searchValue!=null) {
            name = "%"+searchValue+"%";
        }
        // 封装DTO
        RoleDto roleDto = new RoleDto();
        roleDto.setName(name);
        roleDto.setPageSize(pageSize);
        roleDto.setCurrentPageIndex(currentPageIndex);
        return roleDto;
    }


    @Override
    protected void running() {
        System.out.println("RoleListQuery.running");
        roleListView.dataPaneStatusLoading();
        roleListView.dataPaneObservableList.clear();
        roleListView.selectAllBooleanProperty.set(false);
    }

    @Override
    protected void succeeded() {
        System.out.println("RoleListQuery.succeeded");
        // 无数据
        if(page.getPageData().isEmpty()){
            roleListView.dataPaneStatusNoData();
            roleListView.pageCountProperty.set(1);
            roleListView.maxPageIndicatorCount.set(1);
            return;
        }
        // 有数据
        Integer pageCount = page.getPageCount();
        roleListView.pageCountProperty.set(pageCount);
        roleListView.maxPageIndicatorCount.set(PageUtil.getMaxPageIndicatorCount(pageCount));
        roleListView.dataPaneObservableList.setAll(convertToRoleVoList(page.getPageData()));
    }

    private List<RoleVo> convertToRoleVoList(List<Map<String, Object>> pageData) {
        return pageData.stream().map(e -> {
            Object roleIdObj = e.get("role_id");
            Object roleNameObj = e.get("role_name");
            Object permissionNamesObj = e.get("permission_names");
            String roleId = roleIdObj.toString();
            String roleName = roleNameObj.toString();
            String permissionNames = permissionNamesObj == null ? null : permissionNamesObj.toString();
            RoleVo role = new RoleVo();
            role.setId(roleId);
            role.setName(roleName);
            role.setPassword(permissionNames);
            return role;
        }).toList();
    }

    @Override
    protected void failed() {
        System.out.println("RoleListQuery.failed");
        roleListView.dataPaneStatusError();
        roleListView.pageCountProperty.set(1);
        roleListView.maxPageIndicatorCount.set(1);
    }

}
